package com.dzu.medium;

import com.dzu.common.ListNode;

/**
 * @author ZhaoDong
 * @date 2023/3/8 15:42
 * @description 142. 环形链表 II
 */
public class Test142 {
    public ListNode detectCycle(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;

        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;

            if (slow == fast) {
                slow = head;
                while (slow != fast) {
                    slow = slow.next;
                    fast = fast.next;
                }
                return slow;
            }
        }
        return null;
    }
}
